﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_WikiSubCats : System.Web.UI.UserControl
{
    public int Ancestor
    {
        set
        {
            this.ViewState["Ancestor"] = value;
        }

        get
        {
            if (this.ViewState["Ancestor"] == null)
                return 0;
            else
                return (int)ViewState["Ancestor"];
        }
        
    }

    public int TempIDmain
    {
        get
        {
            if (this.ViewState["_TempIDmain"] == null)
                return 0;
            else
                return (int)ViewState["_TempIDmain"];
        }
        set
        {
            this.ViewState["_TempIDmain"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        lblCname.Text = ResourceManage.GetString("Name", "ru");
        lblSubDesc.Text = ResourceManage.GetString("Description", "ru");

        btnBack.Text = ResourceManage.GetString("Back", "ru");
        btnAddCat.Text = ResourceManage.GetString("Add", "ru");
        lblPageName.Text = "Название страницы";

        fillSubCategoryList();
    }

    public void fillSubCategoryList()
    {
        List<CategoryItem> keys = Category.ByAncestors(Ancestor);
        if (keys != null)
        {
            GridView1.Visible = true;
            GridView1.DataSource = keys;
            GridView1.DataBind();
        }
        else
        {
            GridView1.Visible = false;
            //divMsg.Visible = true;
            //divMsgText.Visible = true;
            //divMsgText.InnerText = ResourceManage.GetString("Exception_NoItems", "ru");
        }
    }

    protected void GridView1_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        fillSubCategoryList();
    }

    protected void btnDeleteSubCat_Click(Object sender, CommandEventArgs e)
    {
        int iCatID;
        Int32.TryParse(e.CommandArgument.ToString(), out iCatID);

        GridView1.EditIndex = -1;
        List<CategoryItem> arr = Category.ByAncestors(iCatID);
        if (arr != null)
        {
            foreach (CategoryItem a in arr)
            {
                Category.Delete(a.catId);
                //PageHeaderData.deleteCategory(a.catId);
            }
        }

        Category.Delete(iCatID);
        //PageHeaderData.deleteCategory(id);
        fillSubCategoryList();
        String str = ResourceManage.GetString("Exception_SubCatOnDelete", "ru");
        //' lblMsg.Text = 
        //divMsg.Visible = true;
        //divMsgText.Visible = true;
        //divMsgText.InnerText = str.Replace("{0}", id.ToString());
    }

    protected void gvSubs_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        SubCatView.ActiveViewIndex = 1;
        fillSubCategoryList();
    }

    protected void btnUpdateSubCat_Click(Object sender, CommandEventArgs e)
    {
        int iCatID;
        Int32.TryParse(e.CommandArgument.ToString(), out iCatID);
        
        SubCatView.ActiveViewIndex = 1;

        TempIDmain = iCatID;
        fillCatsDetailsForView();
    }

    private void fillCatsDetailsForView()
    {
        //cleareDetailsView();
        CategoryItem keys = Category.ItemById((int)TempIDmain);
        if (keys != null)
        {
            txtCatname.Text = keys.cTitle;
            txtcDesc.Text = keys.cDescription;
            txtPageName.Text = keys.cPageName;
        }
    }


    protected void btnBack_Click(object sender, EventArgs e)
    {
        //OpenAddCat.Visible = true;
        SubCatView.ActiveViewIndex = 0;
        //fillSubCategoryList();
    }

    protected void btnAddCat_Click(object sender, EventArgs e)
    {
        //OpenAddCat.Visible = false;
        SubCatView.ActiveViewIndex = 1;
    }

    protected void btnUpdateCat_Click(object sender, EventArgs e)
    {
        string sText = txtCatname.Text;
        string sPagename = (txtPageName.Text.Trim().Length > 0) ? txtPageName.Text.Trim() : sText;

        Dal.arParams.Clear();
        Dal.arParams.Add(TempIDmain);
        Dal.arParams.Add(Ancestor); // med centers
        Dal.arParams.Add(sText);
        Dal.arParams.Add(txtcDesc.Text);
        Dal.arParams.Add(ab.ConvertToEnglishText(sPagename));
        Dal.arParams.Add(1);
        Dal.ExecuteDBsp("Admin_AddCategory", Dal.arParams, true);
        //GetCategories();

        //int id = Category.Insert(Ancestor, txtCatname.Text, ab.ConvertToEnglishText(txtCatname.Text));

        fillSubCategoryList();
        SubCatView.ActiveViewIndex = 0;
    }

    // Delete Confirm
    protected void GridView1_RowBound(object sender, GridViewRowEventArgs e)
    {
       
        AjaxControlToolkit.ConfirmButtonExtender cb = new AjaxControlToolkit.ConfirmButtonExtender();
        try
        {
            Button btn = (Button)e.Row.FindControl("lnkDeleteww");


            Int32 iCatID;
            Int32.TryParse(btn.CommandArgument.ToString(), out iCatID);

            cb.TargetControlID = btn.ClientID.Replace("_", "$");
            cb.OnClientCancel = "CancelClick";
            cb.ConfirmText = string.Format("Вы уверены , что хотите удалить запись {0} ?", Category.ItemById(iCatID).cTitle);
            e.Row.Cells[0].Controls.Add(cb);
        }
        catch { }
    }
}
